﻿using System;
using System.Collections.Generic;
using OS.Infrastructure.Domain.UnitOfWork;
using TaskKernel.Configuration;
using TaskKernel.DTO;
using TaskKernel.Domain;
using TaskKernel.Domain.Interface;

namespace TaskWebApplicationUI.PresenterLayer
{
    public class PersonTasksPresenter : AbstractPresenter<IPersonTasksView>
    {
        private readonly IQueryAccount _queryAccount;
        private readonly IQueryTask _queryTask;
        private readonly IUnitOfWorkFactory _uow;
        private Account _currentAccount;

        public PersonTasksPresenter(IQueryAccount queryAccount, IQueryTask queryTask, IUnitOfWorkFactory uow)
        {
            _queryAccount = queryAccount;
            _queryTask = queryTask;
            _uow = uow;
        }

        private Account CurrentAccount
        {
            get
            {
                if (_currentAccount != null)
                    return _currentAccount;
                if (!String.IsNullOrEmpty(View.LogonUser)){
                    _currentAccount = _queryAccount.Get(View.LogonUser);
                    if (_currentAccount != null)
                        return _currentAccount;
                }
                throw new NotImplementedException("Don't know who are you...");
            }
        }

        public void OnViewInitialized()
        {
            using (var z = _uow.Create()) LoadTaskListForExecutorInAllDep();
        }

        private void LoadTaskListForExecutorInAllDep()
        {
            LoadTasksIamExecutor(CurrentAccount);
            var iamParticipantOwner = new List<ExecutorsModel>();
            LoadTasksIamParticipant(CurrentAccount, iamParticipantOwner);
            LoadTasksIamOwner(CurrentAccount, iamParticipantOwner);
            View.IamParticipantAndOwner = iamParticipantOwner;
        }

        private void LoadTasksIamOwner(Account account, IList<ExecutorsModel> iamParticipantOwner)
        {
            var tasks = GetTasksIamOwner(account);
            iamParticipantOwner.Add(tasks.ConvertToCreateExecutorModelItemPersonTaskPresenter(TaskKernelResources.IamOwner,
                                                                                              CurrentAccount));
        }

        private void LoadTasksIamParticipant(Account account, IList<ExecutorsModel> iamParticipantOwner)
        {
            var tasks = GetTasksIamParticipant(account);
            iamParticipantOwner.Add(tasks.ConvertToCreateExecutorModelItemPersonTaskPresenter(TaskKernelResources.IamParticipant,
                                                                                              CurrentAccount));
        }

        private IEnumerable<Task> GetTasksIamExecutor(Account account)
        {
            return _queryTask.GetTasksInProgressWhereIamExecutor(account);
        }

        private IEnumerable<Task> GetTasksIamParticipant(Account account)
        {
            return _queryTask.GetTasksInProgressWhereIamParticipant(account);
        }

        private IEnumerable<Task> GetTasksIamOwner(Account account)
        {
            return _queryTask.GetTasksInProgressWhereIamOwner(account);
        }

        private void LoadTasksIamExecutor(Account account)
        {
            var iamExecutor = new List<ExecutorsModel>();
            var tasks = GetTasksIamExecutor(account);
            iamExecutor.Add(tasks.ConvertToCreateExecutorModelItemPersonTaskPresenter(TaskKernelResources.IamExecutor,
                                                                                      CurrentAccount));
            View.Executors = iamExecutor;
        }
    }
}